
export default {
  name:"AddressAdd",
  components:{},
  data(){
    return {
      name: {
        value:'',
        valid:false
      },
      phone: {
        valid:false,
        number:''
      },
      address: {
        value:'',
        valid:false
      },
      id:-1,
    }
  },
  methods:{
    androidCallJs(val) {
      let obj = this.$getJson(val);
      if(obj.item.id>0){
        this.name.value = obj.item.name;
        this.phone.number = obj.item.phone;
        this.address.value = obj.item.address;
        this.id = obj.item.id;
      }
    },
    inputName(val){
      if(val&&val.length>=8){
        this.name.valid = false;
        this.$vux.toast.text('收货人姓名需1-8字符', 'center');
      }else{
        this.name.valid=true;
      }
    },
    inputAddress(val){
      if(val.length<1){
        this.address.valid = false;
        return
      }
      if(val&&val.length>=60){
        this.address.valid = false;
        this.$vux.toast.text('收货人地址需1-60字符', 'center');
      }else{
        this.address.valid = true;
      }
    },
    checkPhone(value) {
      var reg = /^[1][0-9]{10}$/;
      if (reg.test(value)) {
        this.phone.valid = true;
        return {valid: true}
      } else {
        this.phone.valid = false;
        return {valid: false, msg: "手机号格式错误"}
      }
    },
    save(){
      /*if(this.name.value==""){
        this.$vux.toast.text('收货人姓名不能为空', 'center');
        this.name.valid = false;
        return
      }else if(this.name.value.length>8){
        this.$vux.toast.text('收货人姓名不得大于8字符', 'center');
        this.name.valid = false;
        return
      }
      if(this.phone.number==""){
        this.$vux.toast.text('收货人电话不能为空', 'center');
        return
      }else if(this.phone.number.length>11){
        this.$vux.toast.text('收货人电话不得大于11字符', 'center');
        return
      }
      if(this.address.value==""){
        this.$vux.toast.text('收货人地址不能为空', 'center');
        this.address.valid = false;
        return
      }else if(this.address.value.length>60){
        this.$vux.toast.text('收货人地址不得大于60字符', 'center');
        this.address.valid = false;
        return
      }*/
      if(this.id>0){
        //修改
        let params = {
          addressId:this.id,
          name:this.name.value,
          phone:this.phone.number,
          address:this.address.value
        }
        this.$ajax.post("address/update",params,(res) => {
          if(res.code=='200'){
            this.$vux.toast.text('修改成功', 'center');
            if(this.$fromApp(this)){
              this.$callJava({code:1003,quick :true})
            }else{
              window.location.href = this.$webUrl + "address/manage";
            }
          }else{
            this.$vux.toast.text(res.msg, 'center');
          }
        });
      }else{
        //添加
        let params = {
          name:this.name.value,
          phone:this.phone.number,
          address:this.address.value
        }
        this.$ajax.post("address/create",params,(res) => {
          if(res.code=='200'){
            this.$vux.toast.text('添加成功', 'center');
            if(this.$fromApp(this)){
              this.$callJava({code:1003,quick :true})
            }else{
              // window.location.href = this.$webUrl + "address/manage";
              this.$router.go(-1);
            }
          }else{
            this.$vux.toast.text(res.msg, 'center');
          }
        });
      }

    }
  },
  mounted:function(){
    window['androidCallJs'] = this.androidCallJs;
  }
}
